
import config from './config.js'
import { getUrl } from './index.js'

export default class Request {
	http(param) {
		var method = param.method,
			header = param.header || {},
			hideLoading = param.hideLoadingNew || false;
			
		// 拼接完整请求地址
		var requestUrl;
		if (process.env.NODE_ENV === 'development') {
			requestUrl = '/api' + param.url
		} else {
			requestUrl = config.baseUrl + param.url
		}
		if (method) {
			method = method.toUpperCase(); //小写改为大写
			if (method == "POST" || method == 'PUT') {
				header = {
					'Content-Type': "application/json",
					...header
				};
			} else {
				header = {
					'Content-Type': "application/json",
					...header
				};
				requestUrl = getUrl(requestUrl, param.data)
			}
			// header['tenant-id'] = 132
		}
		//加载圈
		if (hideLoading) {
			uni.showLoading({
				title: '加载中...'
			});
		}
		return new Promise((resolve, reject) => {
			uni.request({
				url: requestUrl,
				data: param.data,
				method: method,
				header: header,
				dataType: 'json',
				success: ({data}) => {
					resolve(data)
				},
				fail: (e) => {
					reject(e)
				},
				complete: () => {
					if (hideLoading) {
						uni.hideLoading();
					}
				}
			})
		})
	}
}
